package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInstAcctRel;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstAcct;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataBilling;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 帐务定制关系构造器
 * @version 1.0
 */
@Component
public class ProdInstAcctRelConstructor extends InstConstructor<KiteProdInstAcct, ProdInstAcctRel> {

    @Override
    public String getTableName() {
        return TableConsts.ProdInstAcctRel.getTableName();
    }

    @Override
    public InstType getParentInstType() {
        return InstType.PROD_INST;
    }

    @Override
    public ProdInstAcctRel build(OrderRequest orderRequest, String prodInstAcctRelId, Map<String, Object> params) {
        if (Objects.isNull(orderRequest) || StringUtils.isBlank(prodInstAcctRelId)) {
            return null;
        }

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteProdInstAcct kiteProdInstAcct = querySceneInstService.getProdInstAcct(orderRequest.getCustId(),
            prodInstAcctRelId);
        KiteProdInst kiteProdInst = querySceneInstService.getProdInstByProdInstAcctRelId(orderRequest.getCustId(),
            prodInstAcctRelId);
        if (isIneffectiveInst(kiteProdInstAcct) || isIneffectiveInst(kiteProdInst)) {
            return null;
        }

        params = Optional.ofNullable(params).orElse(new HashMap<>());
        params.put(KEY_PARENT_INST_TYPE, kiteProdInst.getInstType());
        params.put(KEY_PARENT_INST_ID, kiteProdInst.getProdInstId());

        kiteProdInstAcct.setProdInstId(kiteProdInst.getProdInstId());
        kiteProdInstAcct.setCompInstId(kiteProdInst.getOfferInstId());
        ProdInstAcctRel prodInstAcctRel = convert(orderRequest, kiteProdInstAcct, params);
        InstContainer.ofRequest(orderRequest).addProdInstAcctRel(prodInstAcctRel);

        ProdInst prodInst = buildParentInst(orderRequest, kiteProdInst.getProdInstId());
        if (AcceptUtils.isAddInst(prodInstAcctRel) && Objects.nonNull(prodInst)
            && StringUtils.isNotBlank(prodInst.getOwnerCustId())) {
            prodInstAcctRel.setOwnerCustId(prodInst.getOwnerCustId());
        }

        return prodInstAcctRel;
    }

    @Override
    public ProdInstAcctRel newInst(OrderRequest orderRequest, KiteProdInstAcct kiteProdInstAcct) {
        if (!ObjectUtils.allNotNull(orderRequest, kiteProdInstAcct)) {
            return null;
        }

        Date now = new Date();
        ProdInstAcctRel prodInstAcctRel = KiteBeanUtils.transform(kiteProdInstAcct, ProdInstAcctRel.class,
            KiteInst.ACTION_TYPE);
        prodInstAcctRel.setOwnerCustId(orderRequest.getCustId());
        prodInstAcctRel.setActionType(ActionType.A.name());
        prodInstAcctRel.setStatusDate(now);
        prodInstAcctRel.setCreateStaff(orderRequest.getStaffId());
        prodInstAcctRel.setLanId(orderRequest.getLanId());
        prodInstAcctRel.setEffDate(now);
        prodInstAcctRel.setExpDate(AcceptUtils.getDefaultExpDate());
        prodInstAcctRel.setStatusCd(CoreDataBilling.ACC_0004_1200);
        prodInstAcctRel.setPaymentLimitType(KeyConsts.DEFAULT_PAYMENT_LIMIT_TYPE);
        prodInstAcctRel.setPriority(KeyConsts.DEFAULT_PRIORITY);
        prodInstAcctRel.setLastOrderItemId("-1");
        if (Objects.equals(KeyConsts.ACCT_ITEM_TYPE_GROUP_ID, prodInstAcctRel.getAcctItemGroupId())) {
            prodInstAcctRel.setIfDefaultAcctId(KeyConsts.IF_DEFAULT_ACCT_ID_TRUE);
        }

        return prodInstAcctRel;
    }
}
